package com.example.project.projectspringcloud.controller.admin.video;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.sys.Dictionary;
import com.example.project.projectspringcloud.entity.sys.SysConfig;
import com.example.project.projectspringcloud.entity.user.User;
import com.example.project.projectspringcloud.entity.video.Video;
import com.example.project.projectspringcloud.service.sys.SysConfigService;
import com.example.project.projectspringcloud.service.sys.SysDictionaryService;
import com.example.project.projectspringcloud.service.user.UserService;
import com.example.project.projectspringcloud.service.video.VideoService;
import com.example.project.projectspringcloud.utils.RedisUtil;
import com.example.project.projectspringcloud.utils.StringUtil;
import com.example.project.projectspringcloud.utils.TencentVideoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台视频管理
 */
@Api(tags = "后台视频管理控制器")
@Controller
@RequestMapping(value = "/admin/video")
public class AdminVideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private UserService userService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SysDictionaryService sysDictionaryService;

    @ApiOperation(value = "作品管理列表", notes = "返回作品管理列表")
    @RequestMapping(value = {"", "/list"}, method = {RequestMethod.GET, RequestMethod.POST})
    public String list(Video video, Page page, Model model) throws Exception {
        // 添加排序
        video.setOrderStr("create_date desc");
        List<Video> videoList = videoService.list(page, video);
        model.addAttribute("page", new Page<Video>(videoList));
        model.addAttribute("video", video);

        model.addAttribute("dictionaries", sysDictionaryService.list(new Dictionary("video-type"), null));
        return "admin/video/video-list";

    }

    @ApiOperation(value = "刷新指定视频redis缓存", notes = "刷新指定视频redis缓存")
    @GetMapping(value = "/refresh/{ids}")
    @ResponseBody
    public RespBody refresh(@PathVariable("ids") String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        Integer[] idarr = StringUtil.split(ids, ",");

        videoService.refresh(idarr);
        return RespBody.ok(true);
    }

    @ApiOperation(value = "删除作品", notes = "删除选中作品")
    @DeleteMapping(value = "/delete/{ids}")
    @ResponseBody
    public RespBody delete(@ApiParam(value = "id/ids 1,2,3...") @PathVariable("ids") String ids) {
        if (StringUtils.isBlank(ids)) {
            return RespBody.error(Constants.Message.PARAM_INVALID);
        }
        if (!videoService.delete(StringUtil.split(ids, Constants.Punctuation.COMMA))) {
            return RespBody.error(Constants.Message.DELETE_FAILED);
        }
        return RespBody.ok(Constants.Message.DELETE_SUCCESS);
    }

    @ApiOperation(value = "作品表单", notes = "返回作品表单")
    @GetMapping(value = "/form")
    public String form(@RequestParam(value = "id", required = false) Integer id, Model model) throws Exception {
        if (id != null) {
            Video video = videoService.get(new Video(id));
            model.addAttribute("video", video);
        }

        List<User> users = userService.list(null, new User());
        model.addAttribute("users", users);
        model.addAttribute("dictionaries", sysDictionaryService.list(new Dictionary("video-type"), null));
        return "admin/video/video-form";
    }

    @ApiOperation(value = "保存作品信息", notes = "根据ID判断更新/插入")
    @PostMapping(value = "/save")
    @ResponseBody
    public RespBody save(Video video) throws Exception {
        try {
            boolean result = videoService.save(video);
            if (!result) {
                return RespBody.error(Constants.Message.SAVE_FAILED);
            }
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }

    @ApiOperation(value = "更改首页显示", notes = "更改是否在首页显示")
    @PostMapping(value = "/change_show")
    @ResponseBody
    public RespBody changeshow(Video video) throws Exception {
        try {
            boolean result = videoService.changeshow(video);
            if (!result) {
                return RespBody.error(Constants.Message.SAVE_FAILED);
            }
        } catch (OperationException e) {
            return RespBody.error(e.getMessage());
        }

        return RespBody.ok(Constants.Message.SAVE_SUCCESS);
    }

    @ApiOperation(value = "API修改播放量/点赞/收藏", notes = "修改播放量/点赞/收藏")
    @PostMapping(value = "/api/count")
    @ResponseBody
    public RespBody count(Video video) throws OperationException {
        return RespBody.ok(videoService.save(video));
    }
}
